<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">--</div>
    <script>
        function queueJob() {
            const queue = new Set();
            let p = Promise.resolve();
            let isFlushing = false;
            return function(job) {
                //将job添加到任务队列
                queue.add(job);
                if (!isFlushing) {
                    isFlushing = true;
                    p.then(() => {
                        try {
                            queue.forEach(jo => jo());
                        } catch (e) {
                            console.error("异常", e)
                        } finally {
                            //重置
                            isFlushing = false;
                            queue.length = 0;
                        }
                    })
                }

            }
        }
        let q = queueJob();
        var value = 1;

        function task() {
            //更新dom
            console.log("更新dom")
            document.querySelector("#app").innerHTML = value;
        }

        function doSomeThing() {
            //value每修改一次，就要同步以下html
            value++;
            //task(),,丢弃
            q(task); //改为这样

        }

        doSomeThing();
        doSomeThing();
        doSomeThing();
        doSomeThing();
    </script>
</body>

</html>